<?php
include_once "DBCORE.cls.php";
class GCMUsers
	{
		private $tablename="gcm_users";
		private $db;
		public function __construct()
		{
			$this->db= new DBCORE();
			$this->db->db_connect('localhost','root','','restaurant');
			$this->db->db_query("SET NAMES 'UTF8'");
		}
		
		//
		public function insert($data = array())
		{
			if(empty($data['gcm_regid']) || !isset($data['gcm_regid']))
				return false;
			$id = $data['gcm_regid'];
			$name = $data['name'];
			$email = $data['email'];
			$myStrSQL = "INSERT INTO {$this->tablename} (gcm_regid, name, email, created_at) VALUES ('$id', '$name','$email', NOW())";
			return $this->db->db_query($myStrSQL);
		}
		//
		
		public function resetAutoincrement()
		{
			$myStrSQL = "ALTER TABLE {$this->tablename} AUTO_INCREMENT = 1";
		}
		//
		
		public function select($id='')
		{
			if(empty($id))
				return false;
			$myStrSQL = "SELECT gcm_regid,name,email FROM {$this->tablename} WHERE gcm_regid='$id'";
			$data = $this->db->db_fetch_assoc($this->db->db_query($myStrSQL));
			return data;
		}
		//
		
		public function selectAll()
		{
			$myStrSQL="SELECT gcm_regid FROM {$this->tablename} WHERE 1";
			$result=$this->db->db_query($myStrSQL);
			$resArray = array();
			while ($row = $this->db->db_fetch_array($result)) {
				$resArray[] = $row;
			}
			return $resArray;
		}
		//
		
		public function delete($id='')
		{
			if(empty($id))
				return false;
			$myStrSQL = "DELETE FROM {$this->tablename} WHERE gcm_regid='$id'";
			$this->db->db_query($myStrSQL);
			return $this->db->db_affected_rows();
		}
		
		public function deleteAll()
		{
			$myStrSQL="DELETE FROM {$this->tablename} WHERE 1";
			$this->db->db_query($myStrSQL);
			return $this->db->db_affected_rows();
		}
		public function isHavingKey($keyID)
		{
			if(empty($keyID))
				return 0;
			$myStrSQL = "SELECT * FROM {$this->tablename} WHERE gcm_regid='$keyID'";
			return $this->db->db_num_rows($this->db->db_query($myStrSQL));
			
		}
	}
?>